<?php

namespace App\Admin\Grids;

use App\Admin\Actions\HospitalAppointVaccineDeleteAction;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Admin\Forms\HospitalAppointVaccineForm;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Hospital\Repositories\HospitalAppointVaccine;
use App\Models\Vaccine;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\GridAction;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Support\Facades\DB;

class HospitalAppointVaccineGrid extends LazyRenderable
{

    public function addScript()
    {
        Admin::script( <<<JS

        JS);
    }
    public function grid(): Grid
    {
        $this->addScript();
        $data = $this->payload;
        return Grid::make(new HospitalAppointVaccine(), function (Grid $grid) use($data){

            $grid->disableFilter();
            $grid->disableFilterButton();

            $grid->model()->where('hospital_appoint_id',$data['hospital_appoint_id']);


            $grid->column('vaccine_id')->using(Vaccine::query()->pluck('name','id')->toArray());
            $grid->column('weeks')->display(function($model){
                return json_decode($this->weeks,true);
            })->map(function($value){
                $weeks = [
                    '1' => '周一  ',
                    '2' => '周二  ',
                    '3' => '周三  ',
                    '4' => '周四  ',
                    '5' => '周五  ',
                    '6' => '周六  ',
                    '0' => '周日  ',
                ];
                return $weeks[$value];
            })->badge();

            // 也可以通过这种方式添加
            $grid->actions(function ($actions) {

                $actions->append(new HospitalAppointVaccineDeleteAction());
                //$actions->append(new VaccineIntervalAction());

            });
            $grid->showCreateButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->disableDeleteButton();

            $grid->setResource('hospital-appoint-vaccine');
            $grid->disableFilter();

            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->setDialogFormDimensions('1000px', '100%');

            $grid->model()->setConstraints([
                'hospital_appoint_id' => $data['hospital_appoint_id'],
                'hospital_id' => $data['hospital_id'],
            ]);


        });
    }

}
